package com.ruoyi.system.service.front.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.CooperativeMerchant;
import com.ruoyi.system.domain.PageRecommend;
import com.ruoyi.system.domain.PrizeWinRecord;
import com.ruoyi.system.domain.StudentInfo;
import com.ruoyi.system.domain.front.*;
import com.ruoyi.system.mapper.CasePicMapper;
import com.ruoyi.system.mapper.CooperativeMerchantMapper;
import com.ruoyi.system.mapper.PrizeWinRecordMapper;
import com.ruoyi.system.mapper.front.CaseFrontMapper;
import com.ruoyi.system.mapper.front.MerchantFrontMapper;
import com.ruoyi.system.service.connectUnity.IConnectService;
import com.ruoyi.system.service.connectUnity.impl.ConnectServiceImpl;
import com.ruoyi.system.service.front.ICaseFrontService;
import com.ruoyi.system.service.front.IMerchantFrontService;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MerchantFrontServiceImpl extends CxBase implements IMerchantFrontService {
    @Autowired
    private MerchantFrontMapper merchantFrontMapper;
    @Autowired
    private PrizeWinRecordMapper prizeWinRecordMapper;
    @Autowired
    private CooperativeMerchantMapper cooperativeMerchantMapper;
    @Autowired
    private IConnectService connectService;


    /**
     * 查询页面广告
     *
     * @return 广告内容
     */
    public List<RecommendFrontInfo> selectrecommend(RecommendPage recommendPage){
        return merchantFrontMapper.selectrecommend(recommendPage);
    }

    /** 学生领取相关奖品
     * @return int
     */
    public int updatePrizeType(String QRcodeInfo){
        if(!checkNull(QRcodeInfo)) {
            QRcodeInfo = connectService.decrypt(QRcodeInfo);
            JSONObject jsonObject = JSONObject.parseObject(QRcodeInfo);
            PrizeWinRecord prizeWinRecord = new PrizeWinRecord();
            prizeWinRecord.setStudentId(jsonObject.getString("studentId"));
            prizeWinRecord.setPrizeId(jsonObject.getString("prizeId"));
            prizeWinRecord.setSafetyExaminationId(jsonObject.getString("safetyExaminationId"));
            List<PrizeWinRecord> prizeWinRecords = prizeWinRecordMapper.selectPrizeWinRecordList(prizeWinRecord);
            if(prizeWinRecords.size()>0) {
                if(prizeWinRecords.get(0).getReceiveType().equals("0")) {
                    prizeWinRecord.setReceiveType("1");
                    prizeWinRecord.setPrizeWinPic(getStamp().toString());
                    return merchantFrontMapper.updatePrizeType(prizeWinRecord);
                }
                else if(prizeWinRecords.get(0).getReceiveType().equals("1")){
                    return -1;
                }
            }
        }
        return 0;
    }

    /** 查询商户提供奖品列表数据
     * @return MerchantPrizeInfo
     */
    public List<MerchantPrizeInfo> selectMerchantPrizeInfo(FrontMerchantInfo frontMerchantInfo){
        if(!checkNull(frontMerchantInfo.getMerchantId())){
            frontMerchantInfo.setMerchantId(connectService.decrypt(frontMerchantInfo.getMerchantId()));
            return merchantFrontMapper.selectMerchantPrizeInfo(frontMerchantInfo);
        }
        return null;
    }
    /** 商户登录
     * @return MerchantPrizeInfo
     */
    public FrontMerchantInfo loginMerchant(FrontMerchantInfo frontMerchantInfo){
        if(!checkNull(frontMerchantInfo.getMerchantId())&&!checkNull(frontMerchantInfo.getMerchantPassWord())){
            CooperativeMerchant cooperativeMerchant = new CooperativeMerchant();
            cooperativeMerchant.setMerchantId(frontMerchantInfo.getMerchantId());
            cooperativeMerchant.setMerchantPasswordold(frontMerchantInfo.getMerchantPassWord());
            List<CooperativeMerchant> cooperativeMerchants = cooperativeMerchantMapper.selectCooperativeMerchantList(cooperativeMerchant);
            if(cooperativeMerchants.size()>0){
                FrontMerchantInfo frontMerchantInfo1 = new FrontMerchantInfo();
                frontMerchantInfo1.setMerchantId(connectService.encryption(frontMerchantInfo.getMerchantId()));
                return frontMerchantInfo1;
            }
        }
        return null;
    }
}
